/**
 * 
 */
package com.whu.waterworks.action;

import java.util.ArrayList;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.whu.waterworks.model.WaterPlantNorm;
import com.whu.waterworks.service.WaterPlantNormService;

/**
 * @author： Joymin
 * @date：2016-4-1 上午9:45:52
 * 说明: 
 */
public class WaterPlantNormAction extends ActionSupport{
	private  WaterPlantNormService waterPlantNormService;
	private  WaterPlantNorm norm;
	private  String jonsStrOfName;
	private  String code; //qualityNorm;qualityNonNorm;antisseptic;
	private  ArrayList<String> qualityNorm;
	private  ArrayList<String> qualityNonNorm;
	private  ArrayList<String> antisseptic;
	private  String normType;

	@SuppressWarnings("finally")
	public String addWaterPlantNorm(){
		System.out.println("123456");
		System.out.println("code="+code);
		System.out.println("qualityNorm.size()="+qualityNorm.size());
		String message="";
		ActionContext ctx = ActionContext.getContext();
		JSONObject jsonObj=new JSONObject();
		StringBuilder createVlueOfNorm=new StringBuilder();
		if(null!=qualityNorm &&qualityNorm.size()>0){
			for(int i=0;i<qualityNorm.size();++i){
				createVlueOfNorm.append(qualityNorm.get(i)).append("#");
			}
			createVlueOfNorm.deleteCharAt(createVlueOfNorm.length()-1);
			jsonObj.put("qualityNorm", createVlueOfNorm.toString());
		}
		createVlueOfNorm.setLength(0);
		if(null!=qualityNonNorm &&qualityNonNorm.size()>0){
			for(int i=0;i<qualityNonNorm.size();++i){
				createVlueOfNorm.append(qualityNonNorm.get(i)).append("#");
			}
			createVlueOfNorm.deleteCharAt(createVlueOfNorm.length()-1);
			jsonObj.put("qualityNonNorm", createVlueOfNorm.toString());
		}
		createVlueOfNorm.setLength(0);
		if(null!=antisseptic &&antisseptic.size()>0){
			for(int i=0;i<antisseptic.size();++i){
				createVlueOfNorm.append(antisseptic.get(i)).append("#");
			}
			createVlueOfNorm.deleteCharAt(createVlueOfNorm.length()-1);
			jsonObj.put("antisseptic", createVlueOfNorm.toString());
		}
		WaterPlantNorm norm=new WaterPlantNorm();
		norm.setWaterPlantId(code);
		norm.setSelectedNorm(jsonObj.toString());
		try{
			message=waterPlantNormService.saveWaterPlantNorm(norm);			
		}catch(Exception e){
			e.printStackTrace();
			message="提交失败，请稍后！";
			return message;
		}finally{
			System.out.println(message);		
			ctx.put("message", message);//往request里放入req
			ctx.put("urladdress", "./waterQuality/qualityNorm.jsp");//往request里放入req	
			return SUCCESS;  
		}		
	}
	
	public String getNormForNorm(){
		System.out.println("code="+code);
		System.out.println("normType="+normType);
		String seletedNorm = null;
		WaterPlantNorm norm=waterPlantNormService.getWaterPlantNormById(code);		
		if(null!=norm){
			JSONObject jsonObj= JSONObject.fromObject(norm.getSelectedNorm());
			if(jsonObj.containsKey("qualityNorm")){
				seletedNorm=jsonObj.getString("qualityNorm")+"#";
			}
			if(jsonObj.containsKey("antisseptic")){
				seletedNorm+=jsonObj.getString("antisseptic")+"#";
			}
			if(jsonObj.containsKey("qualityNonNorm")){
				seletedNorm+=jsonObj.getString("qualityNonNorm");
			}			
			 /*if(null==normType){
					if(jsonObj.containsKey("qualityNorm")){
						seletedNorm=jsonObj.getString("qualityNorm")+"#";
					}
					if(jsonObj.containsKey("antisseptic")){
						seletedNorm+=jsonObj.getString("antisseptic")+"#";
					}
					if(jsonObj.containsKey("qualityNonNorm")){
						seletedNorm+=jsonObj.getString("qualityNonNorm");
					}
			}
			else if(normType.equals("0") && jsonObj.containsKey("qualityNorm")){
				seletedNorm=jsonObj.getString("qualityNorm");
			}else if(normType.equals("1") && jsonObj.containsKey("antisseptic")){
				seletedNorm=jsonObj.getString("antisseptic");
			}else if(normType.equals("2") && jsonObj.containsKey("qualityNonNorm")){
				seletedNorm=jsonObj.getString("qualityNonNorm");
			}	*/		
			jonsStrOfName=seletedNorm;
			System.out.println(seletedNorm);
		}
		jonsStrOfName=seletedNorm;
		return SUCCESS;
	}
	
	
	public WaterPlantNormService getWaterPlantNormService() {
		return waterPlantNormService;
	}
	public void setWaterPlantNormService(WaterPlantNormService waterPlantNormService) {
		this.waterPlantNormService = waterPlantNormService;
	}
	public WaterPlantNorm getNorm() {
		return norm;
	}
	public void setNorm(WaterPlantNorm norm) {
		this.norm = norm;
	}
	public String getJonsStrOfName() {
		return jonsStrOfName;
	}
	public void setJonsStrOfName(String jonsStrOfName) {
		this.jonsStrOfName = jonsStrOfName;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	public ArrayList<String> getQualityNorm() {
		return qualityNorm;
	}


	public void setQualityNorm(ArrayList<String> qualityNorm) {
		this.qualityNorm = qualityNorm;
	}


	public ArrayList<String> getQualityNonNorm() {
		return qualityNonNorm;
	}

	public void setQualityNonNorm(ArrayList<String> qualityNonNorm) {
		this.qualityNonNorm = qualityNonNorm;
	}


	public ArrayList<String> getAntisseptic() {
		return antisseptic;
	}


	public void setAntisseptic(ArrayList<String> antisseptic) {
		this.antisseptic = antisseptic;
	}

	public String getNormType() {
		return normType;
	}

	public void setNormType(String normType) {
		this.normType = normType;
	}

}
